package com.zaofan.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zaofan.bean.OpenArea;
import com.zaofan.bean.OpenBuilder;
import com.zaofan.bean.OpenCity;
import com.zaofan.constant.StateConstant;
import com.zaofan.dao.OpenBuilderDao;

public class OpenBuilderService {
	private OpenBuilderDao openBuilderDao = new OpenBuilderDao();
	private ShopBuilderService shopBuilderService = new ShopBuilderService();

	public void delete(OpenBuilder openBuilder) {
		openBuilderDao.del(openBuilder);
	}

	public void add(OpenBuilder openBuilder) {
		openBuilder.setAddTime(new Date());
		OpenCityService cityService = new OpenCityService();
		OpenCity city = cityService.findById(openBuilder.getCityId());
		openBuilder.setCityName(city.getCityName());
		OpenAreaService areaService = new OpenAreaService();
		OpenArea area = areaService.findById(openBuilder.getAreaId());
		openBuilder.setAreaName(area.getAreaName());
		openBuilderDao.add(openBuilder);
		city.setBuilderNum(city.getBuilderNum() + 1);
		area.setBuilderNum(area.getBuilderNum() + 1);
		cityService.update(city);
		areaService.update(area);
	}

	public void update(OpenBuilder openBuilder) {
		OpenCityService cityService = new OpenCityService();
		OpenCity city = cityService.findById(openBuilder.getCityId());
		OpenAreaService areaService = new OpenAreaService();
		openBuilder.setCityName(city.getCityName());
		OpenArea area = areaService.findById(openBuilder.getAreaId());
		openBuilder.setAreaName(area.getAreaName());
		openBuilderDao.update(openBuilder);
		shopBuilderService.updateBuilderName(openBuilder);
	}

	public List<OpenBuilder> findAll(Map<String, Object> params) {
		List<OpenBuilder> data = openBuilderDao.findAll(params);
		if (data == null) {
			data = new ArrayList<>();
		}
		return data;
	}

	public Long count(Map<String, Object> params) {
		return openBuilderDao.count(params);
	}

	public OpenBuilder findById(Integer id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		return openBuilderDao.findOne(params);
	}

	public void updateCityName(OpenCity city) {
		openBuilderDao.updateCityName(city);
	}

	public void updateAreaName(OpenArea area) {
		openBuilderDao.updateAreaName(area);
	}

	public void setBuilderState(OpenBuilder openBuilder) {
		openBuilderDao.updateState(openBuilder);
		OpenCityService cityService = new OpenCityService();
		OpenAreaService areaService = new OpenAreaService();
		OpenCity city = cityService.findById(openBuilder.getCityId());
		OpenArea area = areaService.findById(openBuilder.getAreaId());
		if (openBuilder.getState() == StateConstant.STATE_SHOW) {
			city.setBuilderNum(city.getBuilderNum() + 1);
			area.setBuilderNum(area.getBuilderNum() + 1);
		} else {
			city.setBuilderNum(city.getBuilderNum() - 1);
			area.setBuilderNum(area.getBuilderNum() - 1);
		}
		cityService.update(city);
		areaService.update(area);
	}

	public List<OpenBuilder> findByAreaId(int areaId) {
		Map<String, Object> params = new HashMap<>();
		params.put("areaId", areaId);
		return openBuilderDao.findAll(params);
	}

	public List<OpenBuilder> findByNameLike(String key, String cityId) {
		List<OpenBuilder> builders = openBuilderDao.findByNameLike(key, cityId);
		if (builders == null) {
			builders = new ArrayList<>();
		}
		return builders;
	}

	public List<OpenBuilder> findOpenByAreaId(int areaId) {
		Map<String, Object> params = new HashMap<>();
		params.put("areaId", areaId);
		params.put("state", StateConstant.STATE_SHOW);
		return openBuilderDao.findAll(params);
	}

	public void updateStateUnShowByAreaId(int areaId) {
		openBuilderDao.updateStateUnShowByAreaId(areaId);
	}

	public void updateStateUnShowByCityId(int cityId) {
		openBuilderDao.updateStateUnShowByCityId(cityId);
	}

}
